﻿using FastData;
using M6R小圆柱注液机.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Control;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace M6R小圆柱注液机.Pages
{
    public partial class AdminLoginPage : Form
    {
        public AdminLoginPage()
        {
            InitializeComponent();
        }


        string userName;

        private void Login_Load(object sender, EventArgs e)
        {
            if (cbmGrade.Items.Count != 0)
            {
                cbmGrade.Items.Clear();
            }

            //用户名
            cbmGrade.Items.Add("     管 理 员");
            cbmGrade.Items.Add("     工 程 师");
            cbmGrade.SelectedIndex = 0;

            //初始化                         
            tbPassword.Text = string.Empty;
            this.Show();//先Show 才能设置焦点，下面focus方法才有用
            tbPassword.Focus();
        }

        /// <summary>
        /// 权限级别选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbmGrade_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cbmGrade.Text)
            {
                case "     管 理 员":
                    tbPassword.Text = "";
                    tbPassword.Focus();
                    break;
                case "     工 程 师":
                    tbPassword.Text = "";
                    tbPassword.Focus();
                    break;
                default: break;
            }
        }


        private void btnLogin_Click(object sender, EventArgs e)
        {
            Xml_Helper xml_Helper = new Xml_Helper();

            switch (cbmGrade.Text)
            {
                case "     管 理 员":
                    userName = "Administrator";
                    break;
                case "     工 程 师":
                    userName = "Engineer";
                    break;
                default:
                    break;
            }
            string result = xml_Helper.ReadXml(GlobalValues.PathVariable.fileXML, userName);
            if (result == tbPassword.Text)
            {
                switch (userName)
                {
                    case "Administrator":
                        GlobalValues.StatusVariable.isEngineerLoginSuccess = false;
                        GlobalValues.StatusVariable.isAdminLoginSuccess = true;
                        break;
                    case "Engineer":
                        GlobalValues.StatusVariable.isAdminLoginSuccess = false;
                        GlobalValues.StatusVariable.isEngineerLoginSuccess = true;
                        break;
                    default:
                        break;
                }
                lbStatus.Text = "登录成功";
                lbStatus.ForeColor = Color.Green;
                GlobalValues.log_Helper.DispProcess("登录成功", "Info", true);

                DateTime dtOld = DateTime.Now;
                Task timerTask = Task.Run(() =>
                {
                    DateTime dtNew = DateTime.Now;
                    while (dtOld.AddMinutes(5) > dtNew)
                    {
                        Thread.Sleep(5000);
                        dtNew = DateTime.Now;
                    }
                    GlobalValues.StatusVariable.isAdminLoginSuccess = false;
                    GlobalValues.StatusVariable.isEngineerLoginSuccess = false;
                });

                this.Close();
            }
            else
            {
                switch (userName)
                {
                    case "Administrator":
                        GlobalValues.StatusVariable.isAdminLoginSuccess = false;
                        break;
                    case "Engineer":
                        GlobalValues.StatusVariable.isEngineerLoginSuccess = false;
                        break;
                    default:
                        break;
                }
                lbStatus.Text = "登录失败,请检查";
                lbStatus.ForeColor = Color.Red;
                GlobalValues.log_Helper.DispProcess("登录失败，请检查", "Alarm", true);
                tbPassword.Text = string.Empty;
                tbPassword.Focus();
            }
        }

        private void lbLogout_Click(object sender, EventArgs e)
        {
            GlobalValues.StatusVariable.isAdminLoginSuccess = false;
            GlobalValues.StatusVariable.isEngineerLoginSuccess = false;
            GlobalValues.log_Helper.DispProcess("登录注销", "Info", true);
            this.Close();
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }







        public delegate void delegateMsg(string msg);
        public event delegateMsg eventMsg;



    }
}

